//
//  MTPaymentItemView.swift
//  MT
//
//  Created by ooops on 2020/3/11.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory


protocol MTPaymentItemViewDelegate: NSObjectProtocol {
    /// 选中
    func didSelectedItem(item: MTPaymentItemView)
    
}

class MTPaymentItemView: UIView {
    
    
    
    convenience init(title: String, rmb: String, _ id: Int = 0) {
        self.init(frame: .zero)
        self.titleLabel.attributedText = mtArrtibute(text: title + " ", icon: "icon_gift_display", position: 1)
        self.rmbLabel.text = "¥ " + rmb
        self.id = id
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.borderColor = UIColor.im.xfd387c.cgColor
        layer.borderWidth = 1
        layer.cornerRadius = 5
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        addSubview(titleLabel)
        addSubview(rmbLabel)
    }
    
    //icon_gift_display
    
    private func makeConstraint() {
        titleLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(self.snp.centerY).offset(-5)
        }
        
        rmbLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.snp.centerY).offset(-5)
        }
    }
    
    weak var delegate: MTPaymentItemViewDelegate?
    private var id: Int = 0
    
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.textAlignment = .center
        return l
    }()
    
    lazy var rmbLabel: UILabel = {
        let l = UILabel()
        l.textAlignment = .center
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.im.xfd387c
        return l
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    private func mtArrtibute(text: String, icon: String, position: Int, iconSize: CGSize? = CGSize(width: 15, height: 15), iconYFix: CGFloat = -2, textColor: UIColor = UIColor.im.xfd387c) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: text)
        
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : textColor,
                                                             NSAttributedString.Key.font : PingFangSCMediumFont(16)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let markattch = NSTextAttachment()
        
        markattch.image = UIImage(named: icon)
        
        markattch.bounds = CGRect(x: 0, y: iconYFix, width: iconSize!.width, height: iconSize!.height)
        
        let markattchStr = NSAttributedString(attachment: markattch)
        
        /// icon 插入的位置
        if position == 0 {
            markAttribute.insert(markattchStr, at: 0)
        } else {
            markAttribute.append(markattchStr)
        }
        
        return markAttribute
    
    }
    
    lazy var gLayer: CAGradientLayer = {
        //定义渐变的颜色（从黄色渐变到橙色）
        let topColor = UIColor(hex: 0xFF93B9)
        let buttomColor = UIColor(hex: 0xFF5891)
        let endColor = UIColor(hex: 0xFD387C)
        let gradientColors = [topColor.cgColor, buttomColor.cgColor, endColor.cgColor]
         
        //定义每种颜色所在的位置
        let gradientLocations:[NSNumber] = [0.0, 1.0]
         
        //创建CAGradientLayer对象并设置参数
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
         
        //设置其CAGradientLayer对象的frame，并插入view的layer
        //gradientLayer.frame = self.view.frame
        gradientLayer.cornerRadius = 5
        return gradientLayer
    }()
    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.gLayer.frame = self.bounds
        self.layer.insertSublayer(self.gLayer, at: 0)
        self.delegate?.didSelectedItem(item: self)
        //wechatPay(id: self.id)
        
    }
    
//    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//        super.touchesEnded(touches, with: event)
//        self.gLayer.removeFromSuperlayer()
//    }
//
//
//    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
//        super.touchesCancelled(touches, with: event)
//        self.gLayer.removeFromSuperlayer()
//    }
    
    func removeGLayer() {
        self.gLayer.removeFromSuperlayer()
    }
    
}
